package com.itheima.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.web.mapper.BlogMapper;
import com.itheima.web.mapper.CategoryMapper;
import com.itheima.web.service.BlogService;
import com.itheima.xo.pojo.Blog;
import com.itheima.xo.pojo.Category;
import com.itheima.xo.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.web.service.impl
 * fileName :BlogServiceImpl
 * creationTime :2022-11-07 15:19
 */
@Service("aaa")
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public ArrayList<BlogVo> findAll() {
        ArrayList<BlogVo> blogVos=new ArrayList<>();
        LambdaQueryWrapper<Blog> lqw=new LambdaQueryWrapper<>();
        lqw.isNull(Blog::getDeleted);
        List<Blog> blogs = blogMapper.selectList(lqw);
//        return blogs;
        blogs.forEach(blog->{
            blogVos.add(getBlogVo(blog));
        });



        return blogVos;
    }

    private BlogVo getBlogVo(Blog blog) {
        BlogVo blogVo = new BlogVo();
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryId,blog.getCategoryId());
        lqw.isNull(Category::getDeleted);
        Category category = categoryMapper.selectOne(lqw);
        blogVo.setCategoryName(category.getCategoryName());
        blogVo.setBlogId(blog.getBlogId());
        blogVo.setAuthor(blog.getAuthor());
        blogVo.setTitle(blog.getAuthor());
        blogVo.setCreateTime(blog.getCreateTime());
        blogVo.setUpdateTime(blog.getUpdateTime());
        return blogVo;

    }


}
